var iTime = 119;
var homeUrl = "http://www.2lilu.net";
var storeUrl = "http://store.2lilu.net";
var weixinUrl = "http://m.2lilu.net";
var adminUrl = "http://admin.2lilu.net";
var userUrl = "http://user.2lilu.net";

/*-----------------公共顶部的show-more按钮下的导航菜单显示隐藏效果--------------------*/
$(function () {
    //轮播图片上传，设为主图
    $(".goodspic-uplaod").on("click", ".sheweizhutu", function () {
        $(".goodspic-uplaod").find(".images_zone").find(".deld_zhutu").remove();
        $(".goodspic-uplaod").find(".images_zone").find(".deld_input").remove();
        $(this).parent().append("<a class='deld_zhutu' href='javascript:;' style='top:0;display:block;background-color:#fde3e9;'>已设为主图</a>");
        $(this).parent().append("<input type='hidden' class='deld_input' name='goods_thumb' value='" + $(this).siblings("input").val() + "' />");
    })
    $(".goodspic-uplaod").on("mouseenter", ".images_zone", function () {
        console.log($(this));
        $(this).append("<a class='sheweizhutu' href='javascript:;' style='top:0;display:block;background-color:#fff;'>设为主图</a>");
    })
    $(".goodspic-uplaod").on("mouseleave", ".images_zone", function () {
        $(this).find(".sheweizhutu").remove()
    })
});

//发送手机验证码
function sendMobileCode() {
    var mobile = jQuery.trim($("#mobile").val());
    var captcha = jQuery.trim($("#captcha_mobile").val());
    if(!mobile){
        layer.msg('手机号不能为空！');
        return;
    };
    if(!captcha){
        layer.msg('请输入验证码！');
        return;
    }
    $.ajax({
        url: userUrl + '/public/sent_mobile_yzm',
        data: {mobile: mobile, k: captcha},
        type: "POST",
        dataType: "json",
        success: function (data) {
            if (data.boo) {
                RemainTime();
            }else{
                $("#captcha_mobile_image").attr('src',userUrl+"/Public/verify.html?"+ Math.random()); 
            }
            layer.msg(data.msg);
        }
    })
}

//发送手机验证码
function sendEmailCode() {
    var email = jQuery.trim($("#email").val());
    var captcha = jQuery.trim($("#captcha_email").val());
    if(!email){
        layer.msg('邮箱不能为空！');
        return;
    };
    if(!captcha){
        layer.msg('请输入验证码！');
        return;
    }
    $.ajax({
        url: userUrl + '/public/sent_email_yzm',
        data: {email: email, k: captcha},
        type: "POST",
        dataType: "json",
        success: function (data) {
            if (data.boo) {
                RemainTime();
            }else{
                $("#captcha_email_image").attr('src',userUrl+"/Public/verify.html?"+ Math.random()); 
            }
            layer.msg(data.msg);
        }
    })
}



//手机验证码倒计时
function RemainTime(alerts) {
    var Account;
    document.getElementById('btn_send_sms_code').disabled = true;
    var iSecond, sSecond = "", sTime = "";
    if (iTime >= 0) {
        iSecond = parseInt(iTime % 60);
        iMinute = parseInt(iTime / 60)
        if (iSecond >= 0) {
            if (iMinute > 0) {
                sSecond = iMinute + "分" + iSecond + "秒";
            } else {
                sSecond = iSecond + "秒";
            }
        }
        sTime = sSecond;
        if (iTime == 0) {
            clearTimeout(Account);
            sTime = '获取动态验证码';
            if(alerts){
                sTime = alerts;
            }
            iTime = 59;
            document.getElementById('btn_send_sms_code').disabled = false;
        } else {
            Account = setTimeout(function(){RemainTime(alerts)}, 1000);
            iTime --;
        }
    } else {
        sTime = '没有倒计时';
    }
    document.getElementById('btn_send_sms_code').value = sTime;
}


//用户登陆状态判断
function Sign() {
    if (!Signin) {
        AlertLogin();
        return false;
    }else{
        return true;
    }
}


//用户未登录调用登陆弹框
function AlertLogin() {
    layer.open({
        type: 2,
        area: ['400px', '400px'],
        fixed: false, //不固定
        skin: 'layui-layer-rim', //加上边框
        title: '您尚未登录',
        content: [homeUrl+"/Public/alert_login",'no'],
        end: function () {
                location.reload();
            }
    });
}

//ajax请求
function Aajax(url,dete) {
    var s, c;
    $.ajax({
    url: url,
	type:"post",
        dataType: "json",
        data: dete,
        async: false,
        success: function (result) {
            s = result;
        },
        error: function () {
            layer.msg('网络中断,请重试');
        },
        complete: function () {
        }
    });
    return s;
}

function Aajaxobj(url, dete) {
    var s, c;
    var b = $.ajax({
        url: url,
	type:"post",
        dataType: "json",
        data: dete,
        async: false,
        success: function (result) {
            s = result;
        },
        error: function () {
            layer.msg('网络中断,请重试');
        },
    })
    return s;
}

function getCoupon(obj) {
    var num = obj.data("type");
    $.ajax({
        url:  weixinUrl+"/Coupon/getCoupon.html", //返回数据ajax地址
        data: {"num": num},
        dataType: "json",
        type: 'POST',
        beforeSend: function () {
            layerload = layer.load(1, {
                shade: [0.3, '#000'] //0.1透明度的白色背景
            });
        },
        success: function (result) {
            layer.msg(result['info']);
        },
        error: function () {
            layer.msg('网络异常，请稍后重试');
        },
        complete: function () {
            layer.close(layerload);
            $('.btnns').hide();
            $('.btn').html('<a class="button border-dot" href="{:U(index)}">去卡券包查看</a>');
        }
    });
}



$(function () {
    $(".checkbox label").each(function () {
        var e = $(this);
        e.click(function () {
            if (e.find("input").is(":checked")) {
                e.addClass("active icon-check");
            } else {
                e.removeClass("active icon-check");
            }
        })
    });

    $(".radio label").each(function () {
        var e = $(this);
        e.click(function () {
            e.closest(".radio").find("label").removeClass("active");
            e.addClass("active");
        })
    });


    $(".tab .tab-head .tab-nav li").each(function () {
        var e = $(this);
        var trigger = e.closest(".tab").attr("data-toggle");
        if (trigger == "hover") {
            e.mouseover(function () {
                $showtabs(e)
            });
            e.click(function () {
                return false
            })
        } else {
            e.click(function () {
                $showtabs(e);
                return false
            })
        }
    });
});
$showtabs = function (e) {
    var detail = e.children("a").attr("href");
    e.closest(".tab .tab-nav").find("li").removeClass("active");
    e.closest(".tab").find(".tab-body .tab-panel").removeClass("active");
    e.addClass("active");
    $(detail).addClass("active")
};
//嘟嘟头条滚动动画开始
function autoMove() {
    timer = setInterval(doMove, 5000);
}
function doMove() {
    var obj = $(".index_Roll0_ul1")
    var objli = obj.find("li");
    var top = parseInt(obj.css('top'))||0;
    if (obj.height() - objli.height() > -top) {
        obj.animate({
            top: top -= objli.height()
        }, 1000);
    } else {
        obj.animate({
            top: 0
        }, 1000);
    }
}
//嘟嘟头条滚动动画源首页结束
$(".my_reb_open").click(function () {
    $(".my_reb_div").hide();
})
$(".my_reb_lingqu").click(function () {
    $("#show_reb").show();
    $("#show_reb IMG").attr("src", weixinUrl+"/Public/img/red/2ywmk.jpg");
})
 //倒计时
var timers; //计时器储存变量
function thistime(Nowtime,Futuretime){//时间差处理
	var times=[
	{day:00},    //天
	{hours:00},   //时
	{minutes:00}, //分
	{seconds:00}  //秒
	];
    var date1=new Date(Nowtime);  //现在时间
	var date2=new Date(Futuretime);  //开始时间
	var date3=date2.getTime()-date1.getTime()  //时间差的毫秒数
	times.day=Math.floor(date3/(24*3600))
	var leave1=date3%(24*3600)    //计算天数后剩余的毫秒数
	//计算出小时数
	times.hours=Math.floor(leave1/(3600))
	var leave2=leave1%(3600)        //计算小时数后剩余的毫秒数
	//计算相差分钟数
	times.minutes=Math.floor(leave2/(60))
	var leave3=leave2%(60)      //计算分钟数后剩余的毫秒数
	//计算相差秒数
	times.seconds=leave3;
	return times;
}

function Panic(Nowtime,Futuretime,objname,href){//倒计时输出格式并输出倒计时
	var times= thistime(Nowtime,Futuretime);
	if(times.seconds<0){
		//防止未知bug
		times.day=0;
		times.hours=0;
		times.minutes=0;
		times.seconds=0;
		href.attr('href',href.data('href'));
	}
	if(!objname.find(".day").length>0){  //判断是否显示天数
		times.hours=times.day*24+times.hours;
	}
	if(times.day<10){//输出格式控制
		times.day="0"+times.day
	}
	if(times.hours<10){
		times.hours="0"+times.hours
	}
	if(times.minutes<10){
		times.minutes="0"+times.minutes
	}
	if(times.seconds<10){
		times.seconds="0"+times.seconds
	}
	//输出到页面所需id
	objname.find(".day").text(times.day);
	objname.find(".hours").text(times.hours);
	objname.find(".minutes").text(times.minutes);
	objname.find(".seconds").text(times.seconds);
	Nowtime++;
	if(times.seconds=="00"){
		//防止未知bug
		return;
	}
	timers=setTimeout(function(){Panic(Nowtime,Futuretime,objname,href)},1000)
}
function timersss(Nowtime,Futuretime,objname,href){//调用此方法执行倒计时
    Panic(Nowtime,Futuretime,objname,href)
}
//倒计时结束


//添加收藏
function favorites(fav_id,type,html_type){
    if(!Sign()){
        return;
    }
    var data = Aajax(homeUrl+"/Favorites/favorites",{'fav_type':type,'fav_id':fav_id});
    switch (html_type){
    	case 0:    //个人店铺页面关注样式
    		$(".guanzhu_txt_num"+fav_id).html("已关注("+data.num+")");
    	break;
    	case 1:    //商品详情页面店铺关注样式
    		$(".guanzhu_txt_num"+fav_id).html("已关注");
    	break;
    	case 2:    //商品详情页 商品收藏
    		$(".guanzhu_bj"+fav_id).addClass("on");
    		$(".guanzhu_txt_num"+fav_id).html("已收藏("+data.num+")");
    	break
    	default:
    		break;
    }
    layer.msg(data.n);
}

//收集添加收藏
function mfavorites(fav_id,type,obj){
    var data = Aajax(weixinUrl+"/Favorites/favorites",{'fav_type':type,'fav_id':fav_id});
    if(data.p){
        obj.addClass('yes');
    }
    layer.msg(data.n);
}

//退出js
function out(){
    $.ajax({
        url:homeUrl+"/User/out",
        type:"post",
        data:"000000",
        dataType: "json",
        success:function(data){
            if(data){
                layer.msg("您已安全退出");

                history.go(0);
            }else{
                layer.msg("退出失败请重试");
            }
        },
        error:function(){
            layer.msg("网络连接失败");
        }
    });
}
//退出js
//精准定位
function gettop(bobj,obj) { //精准定位  bobj被定位元素 obj定位元素
	var s={};
	s.o = $(window).scrollTop(); //滚动条滚动距离
	s.h = document.documentElement.clientHeight; //窗口高度
	s.i = -1;  //被定位元素到达定位元素第几个
	if(!!bobj){
		s.t = s.o - bobj.offset().top;
	}
	if(!!obj){
		obj.each(function() {
			if(s.o + s.h / 2 > $(this).offset().top) {
				s.i++;
			}
		})
		if(s.i<0){
			s.i=0;
		}
	}
	return s;
};
//精准定位
//遮罩层
function Mask(){
	$("body").append("<div id='mask' style='position: fixed;z-index: 299;top: 0;left: 0;width: 100%;height: 100%;background-color: rgba(0,0,0,.7);'></div>");
    var start = new Date().getTime();
	while(true) {
		if(new Date().getTime()-start > 1000 || $("#mask").length>0){
			return true;
		};
	};
};
//遮罩层

//敏感字符查询
function checkQuote(str){
    var items= [];
    items.push("|", "\\\\", "<", ">", "/", "<<", ">>", "||", "//","{", "}", "[", "]");
    items.push("select", "delete", "update", "insert", "create", "drop", "alter", "trancate");
    items.push("http","https");
    str = str.toLowerCase();
    for (var i = 0; i < items.length; i++) {
        if (str.indexOf(items[i]) >= 0) {
            return true;
        }
    }
    return false;
}
//敏感字符查询


